using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration.IO;
using Microsoft.EnterpriseManagement.Configuration;
using System.IO;
using System.Text;
using HelpClass;


public class Incident
{
    private EnterpriseManagementGroup mg;
    private ManagementPackClass IncidentClass;
    private EnterpriseManagementObjectProjection IncidentProjection;

    // Management pack references
    private ManagementPack systemMp;
    private ManagementPack Mp;

    public Incident(EnterpriseManagementGroup managementGroup)
    {
        SetupManagementPacks(managementGroup);
        string ID = "Incident{0}";
        IncidentProjection.Object[IncidentClass, "Id"].Value = ID;
    }

    private void SetupManagementPacks(EnterpriseManagementGroup managementGroup)
    {
        mg = managementGroup;
        systemMp = mg.ManagementPacks.GetManagementPack(SystemManagementPack.System);
        Version version = systemMp.Version;
        string keyToken = systemMp.KeyToken;
        systemMp = mg.ManagementPacks.GetManagementPack(SystemManagementPack.System);
        Mp = mg.ManagementPacks.GetManagementPack(new Guid("dd26c521-7c2d-58c0-0980-dac2dacb0900"));
        systemMp = mg.ManagementPacks.GetManagementPack(SystemManagementPack.System);
        IncidentClass = this.mg.EntityTypes.GetClass(new Guid("a604b942-4c7b-2fb2-28dc-61dc6f465c68"));
        IncidentProjection = new EnterpriseManagementObjectProjection(mg, IncidentClass);
    }

    public void Commit()
    {
        IncidentProjection.Commit();
    }
    public DateTime TargetResolutionTime
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "TargetResolutionTime"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "TargetResolutionTime"].Value = value;
        }
    }
    public bool Escalated
    {
        get
        {
            return bool.Parse(IncidentProjection.Object[IncidentClass, "Escalated"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "Escalated"].Value = value;
        }
    }
    public IncidentSourceEnum Source
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "Source"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "Source"].Value = enumValue.Id;
        }
    }
    public IncidentStatusEnum Status
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "Status"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "Status"].Value = enumValue.Id;
        }
    }
    public string ResolutionDescription
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "ResolutionDescription"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ResolutionDescription"].Value = value;
        }
    }
    public bool NeedsKnowledgeArticle
    {
        get
        {
            return bool.Parse(IncidentProjection.Object[IncidentClass, "NeedsKnowledgeArticle"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "NeedsKnowledgeArticle"].Value = value;
        }
    }
    public IncidentTierQueuesEnum TierQueue
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "TierQueue"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "TierQueue"].Value = enumValue.Id;
        }
    }
    public bool HasCreatedKnowledgeArticle
    {
        get
        {
            return bool.Parse(IncidentProjection.Object[IncidentClass, "HasCreatedKnowledgeArticle"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "HasCreatedKnowledgeArticle"].Value = value;
        }
    }
    public IncidentSourceEnum LastModifiedSource
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "LastModifiedSource"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "LastModifiedSource"].Value = enumValue.Id;
        }
    }
    public IncidentClassificationEnum Classification
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "Classification"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "Classification"].Value = enumValue.Id;
        }
    }
    public IncidentResolutionCategoryEnum ResolutionCategory
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "ResolutionCategory"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "ResolutionCategory"].Value = enumValue.Id;
        }
    }
    public int Priority
    {
        get
        {
            return int.Parse(IncidentProjection.Object[IncidentClass, "Priority"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "Priority"].Value = value;
        }
    }


    public void AddAssignedUser(EnterpriseManagementObject assignedUser)
    {
        ManagementPackRelationship assignedTo = this.mg.EntityTypes.GetRelationshipClass("System.WorkItemAssignedToUser", this.Mp);

             // Assign the incident to the user.
        this.IncidentProjection.Add(assignedUser, assignedTo.Target);

        this.IncidentProjection.Overwrite();
    }


    /// <summary>
    /// Sets the user that the incident is assigned to.
    /// </summary>
    public void SetAssignedUser(EnterpriseManagementObject assignedUser)
    {
        ManagementPackRelationship assignedTo = this.mg.EntityTypes.GetRelationshipClass("System.WorkItemAssignedToUser", this.Mp);

        // Remove the previously assigned user if there is one.
        try
        {
            this.IncidentProjection[assignedTo.Target][0].Remove();
        }
        catch (ArgumentOutOfRangeException)
        {
            // No one was previously assigned the incident.
        }

        // Assign the incident to the user.
        this.IncidentProjection.Add(assignedUser, assignedTo.Target);

        this.IncidentProjection.Overwrite();
    }

    public System_WorkItem_TroubleTicket_ImpactEnum Impact
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "Impact"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "Impact"].Value = enumValue.Id;
        }
    }
    public System_WorkItem_TroubleTicket_UrgencyEnum Urgency
    {
        set
        {
            ManagementPack enumMp = IncidentProjection.Object[IncidentClass, "Urgency"].Type.GetManagementPack();
            ManagementPackElementCollection<ManagementPackEnumeration> enums = enumMp.GetEnumerations();
            ManagementPackEnumeration enumValue = enums.GetItem(value.ToString().Replace('_', '.'));
            IncidentProjection.Object[IncidentClass, "Urgency"].Value = enumValue.Id;
        }
    }
    public DateTime ClosedDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ClosedDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ClosedDate"].Value = value;
        }
    }
    public DateTime ResolvedDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ResolvedDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ResolvedDate"].Value = value;
        }
    }
    public string Id
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "Id"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "Id"].Value = value;
        }
    }
    public string Title
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "Title"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "Title"].Value = value;
        }
    }
    public string Description
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "Description"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "Description"].Value = value;
        }
    }
    public string ContactMethod
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "ContactMethod"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ContactMethod"].Value = value;
        }
    }
    public DateTime CreatedDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "CreatedDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "CreatedDate"].Value = value;
        }
    }
    public DateTime ScheduledStartDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ScheduledStartDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ScheduledStartDate"].Value = value;
        }
    }
    public DateTime ScheduledEndDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ScheduledEndDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ScheduledEndDate"].Value = value;
        }
    }
    public DateTime ActualStartDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ActualStartDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ActualStartDate"].Value = value;
        }
    }
    public DateTime ActualEndDate
    {
        get
        {
            return DateTime.Parse(IncidentProjection.Object[IncidentClass, "ActualEndDate"].Value.ToString());
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "ActualEndDate"].Value = value;
        }
    }
    public string DisplayName
    {
        get
        {
            return IncidentProjection.Object[IncidentClass, "DisplayName"].Value.ToString();
        }
        set
        {
            IncidentProjection.Object[IncidentClass, "DisplayName"].Value = value;
        }
    }

    
}
